package com.it.bigdata.dao;

import com.it.bigdata.domain.TieZi;
import com.it.bigdata.util.HbaseUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.List;

public class TieZiDao {
    //数据持久化   把一个帖子持久化到数据库
    /*public void add(TieZi tieZi) throws IOException {
        Connection conn = HbaseUtil.getConn();
        Table tz = conn.getTable(TableName.valueOf("tz"));
        //行键====>   title,author,timestamp
        byte[] row = Bytes.toBytes(tieZi.getTitle() + "," + tieZi.getAuthor() + "," + System.currentTimeMillis());
        byte[] family = Bytes.toBytes("base_info");
        Put put = new Put(row);

        if (tieZi.getTitle() != null && tieZi.getAuthor() != null && tieZi.getImages() != null && tieZi.getImages().size() > 0) {
            put.addColumn(family, Bytes.toBytes("title"), Bytes.toBytes(tieZi.getTitle()));
            put.addColumn(family, Bytes.toBytes("author"), Bytes.toBytes(tieZi.getAuthor()));
            StringBuffer sb = new StringBuffer();
            for (String imageUrl : tieZi.getImages()) {
                sb.append(imageUrl + ",");
            }
            String urls = sb.substring(0, sb.length() - 1);
            put.addColumn(family, Bytes.toBytes("images"), Bytes.toBytes(urls));
            tz.put(put);
        }

        //释放资源
        tz.close();
        conn.close();
    }*/
}
